package com.cashbook.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cashbook.pojo.TbArticle;
import com.cashbook.service.ArticleService;

import entity.PageResult;
import entity.Result;

/**
 * controller
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	/**
	 * 返回全部列表
	 * 
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbArticle> findAll() {
		return articleService.findAll();
	}

	/**
	 * 返回全部列表
	 * 
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(int page, int rows) {
		return articleService.findPage(page, rows);
	}

	/**
	 * 增加
	 * 
	 * @param article
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody TbArticle article) {
		try {
			Date d = new Date();
			// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			article.setTime(d);
			articleService.add(article);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}

	/**
	 * 修改
	 * 
	 * @param article
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody TbArticle article) {
		try {
			articleService.update(article);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}

	/**
	 * 获取实体
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public TbArticle findOne(Long id) {
		return articleService.findOne(id);
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long[] ids) {
		try {
			articleService.delete(ids);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}

	/**
	 * 查询+分页
	 * 
	 * @param brand
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbArticle article, int page, int rows) {
		return articleService.findPage(article, page, rows);
	}

	/**
	 * 条件查询 可以通过日期 、文章类型、文章分类
	 * 
	 * @param brand
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/criteria")
	public List<TbArticle> search(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cateId = request.getParameter("cateId");
		String beginTime = request.getParameter("beginTime");
		String endTime = request.getParameter("endTime");
		String title = request.getParameter("title");

		return articleService.findBycriteria(cateId, beginTime, endTime, title);
	}

	/**
	 * 通过分类查询文章
	 * 
	 * @param cateId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findcateId")
	public List<TbArticle> search(Long cateId) throws Exception {
		return articleService.findByCateId(cateId);
	}
	
}
